//package wmmd.disc.webmaster;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import db.DatabaseConnector;

public class ShowWholeMessage extends HttpServlet {
    
    //----------------------------------------------------------------------------------------
    /**
     * Called by displaymessage.jsp - used to respond to a message
     */
    
//doPost method included for POST requests
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        doGet(request, response);
    }
    
//main doGet method
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        //The JSP document passes in which db table to use
        String tName = request.getParameter("tName");  // disc board table
        //int lRTID =  Integer.parseInt(request.getParameter("rtidNum"));
        //Vincent--why was this retrieving the wrong parameter name??
        if(request.getParameter("RTID") != null)
        {
            int lRTID =  Integer.parseInt(request.getParameter("RTID"));
        
            // Variables for the database connection
            DatabaseConnector con = null;
        
            //Set the database connection
            con = new DatabaseConnector(DatabaseConnector.getDatabaseType());

            String SQL = "SELECT ID,Name,Date,Subject,Message FROM " + tName + " WHERE ID = ?";
            String[][] rs = con.selectSQL(SQL,5,new Object[]{lRTID});

            con.CleanUp();

            // Display the contents
            for (int i=0;i<rs.length;i++){
                String id_string = rs[i][0];
                String nameStr = rs[i][1];
                String timeStamp = rs[i][2];
                String subject_string = rs[i][3];
                String message_string = rs[i][4];
                String myDate = timeStamp;
                
                message_string = replace(message_string, "\n\n", "<br><br>");
                message_string = replace(message_string, "\r\n\r\n", "<br><br>");
                message_string = replace(message_string, "\n\t", "<br><br>");
                message_string = replace(message_string, "\r\n\t", "<br><br>");
                //nameStr = nameStr.substring(0, (nameStr.length() - 2));
                
                out.println("<form method=\"post\" action=\"postform.jsp\">");
                out.println("<input type=\"hidden\" name=\"RTID\" value=\"" + id_string + "\">");
                out.println("<input type=\"hidden\" name=\"NAME\" value=\"" + request.getRemoteUser() + "\">");
                out.println("<p><strong>Subject: " + subject_string + "</strong><br>");
                out.print("<em>posted by " + nameStr + " on " + myDate + "</em></p>");
                out.println("<p>" + message_string.trim() + "</p>");
                out.println("<p><hr /></p>");
                
                out.print("<p><strong>You can respond to " + nameStr + " below</strong><br>");
                out.println("(When you are finished, press 'Send Response')</p>");
                out.println("<p><b>Subject:</b><br>");
                out.println("<INPUT TYPE=\"TEXT\" SIZE=\"50\" MAXLENGTH=\"50\" NAME=\"SUBJECT\" VALUE=\"");
                out.println(subject_string + "\"></p>");
                out.println("<p><b>Your Response:</b><br>");
                out.println("<TEXTAREA NAME=\"MESSAGE\" ROWS=\"8\" COLS=\"50\" onFocus=\"if(!this.initialyCleared){this.value='';this.initialyCleared = true;}\">");
                out.println("Type your message here</TEXTAREA></p>");
                out.println("<INPUT TYPE=\"SUBMIT\" name=\"SSC\" value=\"Send Response\">");
                out.println("</form>");
            }
        }
        else
        {

            //Get the person's username from the REMOTE_USER environment variable to set as the name.
            String UName = request.getRemoteUser();
            if(UName==null)
                UName = "Guest";
            out.println("<H2>New Topic</H2>");
            out.print("<P>To submit a new topic, fill out the form below.");
            out.print("When you are satisfied, Click on the button that");
            out.println(" says 'Post New Topic' </P><HR>");
            out.println("<form method=\"POST\" action=\"postform.jsp\">");
            out.println("<input type=\"HIDDEN\" name=\"RTID\" value=\"0\">");
            out.println("<table width=\"100%\" border=\"3\">");
            out.println("<tr><td align=\"right\">Name:</td>");
            out.println("<td align = left>");
            out.println("<input type=\"text\" readonly=\"readonly\" name=\"NAME\" value=\"" + UName + "\"></td>");
            out.println("<td align=\"right\">Topic:</td><td align=\"left\">");
            out.println("<input type=\"text\"  name=\"SUBJECT\" value=\"Type Topic Here\" onFocus=\"if(!this.initialyCleared){this.value='';this.initialyCleared = true;}\">");
            out.println("</td></tr>");
            out.println("<tr><td align=\"right\">Starting Message</td>");
            out.println("<td colspan=\"3\" align=\"left\">");
            out.println("<textarea name=\"MESSAGE\" COLS=\"60\" rows=\"10\" WRAP onFocus=\"if(!this.initialyCleared){this.value='';this.initialyCleared = true;}\">");
            out.println("Type the message to start the topic here</textarea></td></tr>");
            out.println("</table>");
            out.println("<input type=\"SUBMIT\" name=\"SSC\" value=\"Post New Topic\">");
            out.print("<input type=\"RESET\" value=\"Clear Fields\">");
            out.print("</FORM>");
        }
    }
    
    private String replace(String str, String pattern, String replace) {
        int s = 0;
        int e = 0;
        StringBuffer result = new StringBuffer();
        
        while ((e = str.indexOf(pattern, s)) >= 0) {
            result.append(str.substring(s, e));
            result.append(replace);
            s = e+pattern.length();
        }
        result.append(str.substring(s));
        return result.toString();
    }
}
